package com.ellricky.teacher

import android.app.Activity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.adapter.SmartViewHolder
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ResUtil
import com.andr.common.util.StrUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.entity.new.ClassRecordData
import com.ellricky.baselib.entity.new.MsgTip
import com.ellricky.baselib.entity.new.School
import com.ellricky.baselib.entity.new.TeachLeave

internal class TodayCourseAdapter :
    BaseRecyclerAdapter<TcCourseEntity>(R.layout.base_adapter_common_list) {

    override fun onBindViewHolder(vh: SmartViewHolder?, p1: TcCourseEntity?, position: Int) {


        val itemView = vh?.itemView
        itemView?.let {
            val rootView = it.findViewById<CardView>(R.id.cvRoot)
            val tvtitle = it.findViewById<TextView>(R.id.tvTitle)
            val tvValue = it.findViewById<TextView>(R.id.tvValue)
            val tvDate = it.findViewById<TextView>(R.id.tvDate)
            val ivEnd = it.findViewById<ImageView>(R.id.ivEnd)

            var selected = false
            when (p1?.isOverTime) {
                // 1的时候背景显示黑色，2的时候显示白色
                "1" -> {
                    selected = true
                }
            }
            rootView?.isSelected = selected
            tvtitle.isSelected = selected
            tvValue.isSelected = selected
            tvDate.isSelected = selected
            if (selected) ivEnd.setImageResource(R.drawable.base_icon_common_list_white)
            else ivEnd.setImageResource(R.drawable.base_icon_common_list)


            tvtitle.text = p1?.classDate
            tvValue.text = p1?.className
            tvDate.text = p1?.roomName

        }

    }

}


internal class AmMeetAdapter(private val context: Activity) :
    BaseRecyclerAdapter<TcAmMeetEntity>(R.layout.base_adapter_common_list) {

    override fun onBindViewHolder(vh: SmartViewHolder?, p1: TcAmMeetEntity?, position: Int) {

        vh?.text(R.id.tvTitle, p1?.meetTitle)

        vh?.text(R.id.tvKey, context.getString(R.string.tc_fz_teacher))
        vh?.text(R.id.tvValue, p1?.name)

        vh?.text(R.id.tvDateKey, context.getString(R.string.tc_am_date))
        vh?.text(R.id.tvDate, p1?.meetDate)
    }

}

internal class LeaveAdapter(private val context: Activity) :
    BaseRecyclerAdapter<TeachLeave>(R.layout.tc_adapter_leave_record) {

    override fun onBindViewHolder(vh: SmartViewHolder?, p1: TeachLeave?, position: Int) {

        vh?.text(R.id.tvDate, p1?.leave_start_date)
        vh?.text(R.id.tvLeaveType, p1?.leave_type_name)

        vh?.text(R.id.tvReason, p1?.leave_desc)
        vh?.text(R.id.tvTempTeacher, p1?.replace_user_name)

        vh?.itemView?.let {
            val tvState = it.findViewById<TextView>(R.id.tvState)
            when (p1?.leave_state_code) {// 1=未审批 2=同意 3=拒绝
                1 -> {
                    tvState.setTextColor(ResUtil.getColor(context, R.color.tc_yellow))
                    tvState.text = "审批中"
                }
                2 -> {
                    tvState.setTextColor(ResUtil.getColor(context, R.color.tc_green))
                    tvState.text = "通过"
                }

                3 -> {
                    tvState.setTextColor(ResUtil.getColor(context, R.color.tc_red))
                    tvState.text = "拒绝"
                }
            }
        }
    }
}


internal class TcParentMeetAdapter(private val context: Activity) :
    BaseRecyclerAdapter<TcParentMeetEntity>(R.layout.base_adapter_common_list) {

    override fun onBindViewHolder(vh: SmartViewHolder?, p1: TcParentMeetEntity?, position: Int) {

        vh?.text(R.id.tvTitle, p1?.className)

        vh?.text(R.id.tvKey, context.getString(R.string.tc_fz_teacher))
        vh?.text(R.id.tvValue, p1?.name)

        vh?.text(R.id.tvDateKey, context.getString(R.string.tc_meeting_date))
        vh?.text(R.id.tvDate, "${p1?.meetDate} ${p1?.meetTime}")
    }

}

internal class SumupAdapter :
    BaseRecyclerAdapter<TcSumupEntity>(R.layout.tc_adapter_sum_up) {

    override fun onBindViewHolder(vh: SmartViewHolder?, p1: TcSumupEntity?, position: Int) {
        vh?.text(R.id.tvDate, p1?.experienceDate)
    }

}


internal class WorkJoinAdapter(private val context: Activity) :
    BaseRecyclerAdapter<WorkJoinEntity>(R.layout.base_adapter_common_list) {

    override fun onBindViewHolder(vh: SmartViewHolder?, p1: WorkJoinEntity?, position: Int) {

        vh?.text(R.id.tvTitle, p1?.fromName)

        vh?.text(R.id.tvKey, context.getString(R.string.tc_work_join_to))
        vh?.text(R.id.tvValue, p1?.toName)

        vh?.text(R.id.tvDateKey, context.getString(R.string.tc_work_join_date))
        vh?.text(R.id.tvDate, p1?.changeToDate)
    }

}


internal class EvaluateCourseAdapter(private val ctx: Activity) :
    BaseRecyclerAdapter<EvaluateCourseEntity>(R.layout.base_adapter_common_list) {
    override fun onBindViewHolder(vh: SmartViewHolder?, p1: EvaluateCourseEntity?, position: Int) {

        vh?.text(R.id.tvTitle, p1?.className)

        vh?.text(R.id.tvKey, "")
        vh?.text(
            R.id.tvValue, ResUtil.getStr(
                ctx, R.string.tc_stu_evaluate_list,
                p1?.alreadyUsed, p1?.attendNum
            )
        )

        vh?.text(R.id.tvDateKey, "")
        vh?.text(R.id.tvDate, p1?.classDate)
    }

}


internal class MyCourseAdapter(private val ctx: Activity, private val pageType: Int) :
    BaseRecyclerAdapter<MyCourseEntity>(R.layout.tc_adapter_my_course) {

    private val colorBlack = ContextCompat.getColor(ctx, R.color.base_list_black)
    private val colorWhite = ContextCompat.getColor(ctx, R.color.common_main_white)

    override fun onBindViewHolder(vh: SmartViewHolder?, p1: MyCourseEntity?, position: Int) {


        vh?.itemView?.let {

            val rlItemTitle = it.findViewById<RelativeLayout>(R.id.rlWeek)
            val tvWeek = it.findViewById<TextView>(R.id.tvWeek)
            val tvWeekDay = it.findViewById<TextView>(R.id.tvWeekDay)

            val rootView = it.findViewById<CardView>(R.id.cvRoot)
            val tvTitle = it.findViewById<TextView>(R.id.tvTitle)
            val tvValue = it.findViewById<TextView>(R.id.tvValue)
            val tvDate = it.findViewById<TextView>(R.id.tvDate)

            val ivEnd = it.findViewById<ImageView>(R.id.ivEnd)

            val tvEndContainer = it.findViewById<LinearLayout>(R.id.tvEndContainer)
            val tvTotalNum = it.findViewById<TextView>(R.id.tvTotalNum)
            val tvAttendNum = it.findViewById<TextView>(R.id.tvAttendNum)

            val hasBk = when (StrUtil.filterEmpty(p1?.attend, "0")) {
                "1" -> true
                else -> false
            }
            if (hasBk) {
                // 已备课
                rootView?.setCardBackgroundColor(colorBlack)
                tvTitle.setTextColor(colorWhite)
                tvValue.setTextColor(colorWhite)
                tvDate.setTextColor(colorWhite)
                tvTotalNum.setTextColor(colorWhite)
                tvAttendNum.setTextColor(colorWhite)
            } else {
                // 未备课
                rootView?.setCardBackgroundColor(colorWhite)
                tvTitle.setTextColor(colorBlack)
                tvValue.setTextColor(colorBlack)
                tvDate.setTextColor(colorBlack)
                tvTotalNum.setTextColor(colorBlack)
                tvAttendNum.setTextColor(colorBlack)
            }

            when (pageType) {
                FRAGMENT_COURSE_VIP, FRAGMENT_COURSE_WEEK_VIP -> { // 会员课

                    // 顶部日期显示
                    val hideItemTitle =
                        EmptyUtil.isEmpty(p1?.week) && EmptyUtil.isEmpty(p1?.weekDay)
                    if (hideItemTitle) {
                        rlItemTitle.visibility = View.GONE
                    } else {
                        rlItemTitle.visibility = View.VISIBLE
                        tvWeek.text = p1?.week
                        tvWeekDay.text = p1?.weekDay
                    }

                    val courseName = StrUtil.filterEmpty(p1?.classTypeName)
                    val classTime = StrUtil.filterEmpty(p1?.classDate)
                    var title = classTime
                    if (pageType == FRAGMENT_COURSE_WEEK_VIP) {
                        title = courseName.plus(classTime)
                    }
                    if (!hasBk) {
                        title = title.plus("（未备课）")
                        tvEndContainer.visibility = View.GONE
                        ivEnd.visibility = View.VISIBLE
                    } else {
                        tvEndContainer.visibility = View.VISIBLE
                        ivEnd.visibility = View.GONE
                        tvTotalNum.text = "应到".plus(StrUtil.filterEmpty(p1?.totalNum)).plus("人")
                        tvAttendNum.text = "实到".plus(StrUtil.filterEmpty(p1?.attendNum)).plus("人")
                    }

                    tvTitle.text = title
                    tvValue.text = StrUtil.filterEmpty(p1?.className)
                    tvDate.text = StrUtil.filterEmpty(p1?.roomName)


                }
                else -> {
                    tvTitle.text = p1?.className
                    tvValue.text = StrUtil.filterEmpty(p1?.totalNum).plus("人")
                    tvDate.text = StrUtil.filterEmpty(p1?.startDate).plus(
                        StrUtil.filterEmpty(p1?.classDate)
                    )

                    tvTotalNum.text = "应到".plus(StrUtil.filterEmpty(p1?.totalNum)).plus("人")
                    tvAttendNum.text = "实到".plus(StrUtil.filterEmpty(p1?.attendNum)).plus("人")
                }
            }

        }
    }


    internal class ChangeSchoolAdapter(private val context: Activity) :
        BaseRecyclerAdapter<School>(R.layout.base_adapter_change_school) {

        override fun onBindViewHolder(vh: SmartViewHolder, p1: School, position: Int) {
            vh.text(R.id.tvSchool, p1.school_name)
        }
    }

    internal class ClassRecordAdapter(private val context: Activity) :
        BaseRecyclerAdapter<ClassRecordData>(R.layout.base_adapter_class_record) {

        override fun onBindViewHolder(vh: SmartViewHolder, p1: ClassRecordData, position: Int) {
            vh.text(R.id.tv_create_time, p1.teach_createtime)
                .text(R.id.tv_class_name, "班级:${p1.class_name}")
                .text(R.id.tv_course_num, "+" + p1.teach_course_num.toString())
                .text(R.id.tv_course_name, p1.course_type_name)
                .text(R.id.tv_job_state, p1.is_job_ok_info)

            vh.itemView.findViewById<TextView>(R.id.tv_job_state).apply {
                if (p1.is_job_ok == BaseConstant.JOB_UPLOAD_SUCCESS) {
                    setTextColor(ContextCompat.getColor(context, R.color.tc_green))
                } else {
                    setTextColor(ContextCompat.getColor(context, R.color.base_list_black))
                }
            }
        }
    }
}